package com.woniu.customer.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.customer.pojo.ReconsigneesUnit;
import com.woniu.customer.service.ReconsigneesUnitService;
import com.woniu.customer.mapper.ReconsigneesUnitMapper;
import com.woniu.until.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
* @author ajl
* @description 针对表【ecp_clients_reConsignees_unit】的数据库操作Service实现
* @createDate 2023-10-31 19:41:57
*/
@Service
public class ReconsigneesUnitServiceImpl extends ServiceImpl<ReconsigneesUnitMapper, ReconsigneesUnit>
    implements ReconsigneesUnitService{

    @Value("${snowflake.datacenterId}")
    private long datacenterId;
    @Value("${snowflake.workerId}")
    private long workerId;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public String addReConsignees(ReconsigneesUnit reconsignees) {
        save(reconsignees);
        return "添加收货收票单位成功";
    }

    @Override
    public ReconsigneesUnit queryByCustomerId(Long customerId) {
        LambdaQueryWrapper<ReconsigneesUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ReconsigneesUnit::getCustomerName,ReconsigneesUnit::getDeliveryAddress,ReconsigneesUnit::getTransportType,
                ReconsigneesUnit::getConsignee,ReconsigneesUnit::getPhone)
                .eq(ReconsigneesUnit::getCustomerId,customerId);
        ReconsigneesUnit reconsigneesUnit = getOne(queryWrapper);
        return reconsigneesUnit;
    }

    @Override
    public ResponseData<?> addReConsigneesRedis(ReconsigneesUnit reconsignees) {
        Long customerId = IdUtil.getSnowflake(workerId, datacenterId).nextId();
        reconsignees.setCustomerId(customerId);
        redisTemplate.opsForHash().put(String.valueOf(reconsignees.getConsigneeUnit()),String.valueOf(reconsignees.getCustomerId()), JSON.toJSONString(reconsignees));
        return new ResponseData<>().ok();
    }
}




